class Solution
{
public:
    int numSubarrayProductLessThanK(vector<int> &nums, int k)
    {
        int product = 1;
        int result = 0;
        int i = 0;
        int j = 0;
        int n = nums.size();
        while (j < n)
        {
            product *= nums[j];
            while (product >= k)
            {
                if (i == j)
                {
                    product = 1;
                    ++i;
                    break;
                }
                product /= nums[i];
                ++i;
            }
            int len = j - i + 1;
            result += len;
            ++j;
        }
        return result;
    }
};